/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.activity.controller.b;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.activity.dto.CnActivityOrderDTO;
import com.ykht.proj.activity.entity.CnActivityOrder;
import com.ykht.proj.activity.service.CnActivityOrderService;
import com.ykht.proj.activity.vo.CnActivityOrder4GetByVerificationCode;
import com.ykht.proj.admin.api.feign.RemoteUserService;
import com.ykht.proj.admin.api.vo.UserVO;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.annotation.Inner;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.UserTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 活动订单表
 *
 * @author csg
 * @date 2020-12-23 11:13:32
 */
@RestController
@AllArgsConstructor
@RequestMapping("/b/cnactivityorder/")
@Api(value = "cnactivityorder", tags = "活动订单表管理")
@Slf4j
public class BnActivityOrderController {

	private final CnActivityOrderService cnActivityOrderService;
	/**
	 * 订单列表（分页，搜索）
	 *
	 * @param page
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "订单列表（分页，搜索）")
	@SysLog("订单列表（分页，搜索）")
	@GetMapping("getCnActivityOrderListPage")
	public R getCnActivityOrderListPage(Page page, CnActivityOrderDTO req) {
		try {
			log.info("->订单列表（分页，搜索）", JSONUtil.toJsonStr(req));
			return R.ok(cnActivityOrderService.getCnActivityOrderListPage(page, req));
		} catch (Exception e) {
			e.printStackTrace();
			return R.failed();
		}
	}

	/**
	 * 根据核销码 获取订单详情
	 *
	 * @param verificationCode
	 * @return
	 */
	@ApiOperation(value = "根据核销码 获取订单详情")
	@SysLog("根据核销码 获取订单详情")
	@GetMapping("getByVerificationCode")
	public R<CnActivityOrder4GetByVerificationCode> getByVerificationCode(String verificationCode) {
		try {
			log.info("->根据核销码 获取订单详情", verificationCode);

			CnActivityOrder vo = cnActivityOrderService.getOne(new QueryWrapper<CnActivityOrder>().lambda().eq(CnActivityOrder::getVerificationCode, verificationCode));
			CnActivityOrder4GetByVerificationCode o = new CnActivityOrder4GetByVerificationCode();
			BeanUtil.copyProperties(vo,o);
			if (SecurityUtils.getUser().getUserType() == UserTypeEnum.WAITER.getValue()) o.setUserName(SecurityUtils.getUser().getNickName());//服务员
			if (SecurityUtils.getUser().getUserType() == UserTypeEnum.shop.getValue()) o.setUserName(SecurityUtils.getUser().getUsername());//门店
			return R.ok(o);
		} catch (Exception e) {
			e.printStackTrace();
			return R.failed();
		}
	}


	/**
	 * 下载搜索 订单结算数据
	 *
	 * @param req
	 */
	@ApiOperation(value = "下载搜索 订单结算数据")
	@GetMapping("/exportTemp")
	@Inner(false)
	public void exportTemp(CnActivityOrderDTO req) {
		cnActivityOrderService.downloadExcel(req);
	}


}
